﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace _4_05_Directoryinfo对文件夹类操作
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DirectoryInfo directoryInfo1 = new DirectoryInfo(this.textBox1.Text);
            DirectoryInfo directoryInfo2 = new DirectoryInfo(this.textBox2.Text);
            if (directoryInfo1.Exists && directoryInfo2.Exists)
            {
                string[] path = this.textBox1.Text.Split('\\');
                string newPath = this.textBox2.Text + '\\' + path[path.Length - 1];
               DirectoryInfo NewdirectoryInfo = new DirectoryInfo(newPath);
                if (NewdirectoryInfo.Exists)
                {
                    DialogResult dr = MessageBox.Show("目标文件夹已存在，是否覆盖", "提示: ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        //强制删除目标文件夹以及子文件夹
                        NewdirectoryInfo.Delete(true);
                        directoryInfo1.MoveTo(newPath);
                        MessageBox.Show("移动成功");
                    }
                    return;

                }
                directoryInfo1.MoveTo(newPath);
                MessageBox.Show("移动成功");
            }
            else
            {
                MessageBox.Show("文件夹不存在");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DirectoryInfo directoryInfo2 = new DirectoryInfo(this.textBox1.Text);
            directoryInfo2.Delete(true);
            MessageBox.Show("删除成功");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
